using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using MoveIt.Framework;
using MoveIt.Object.Player;
using MoveIt.Object.Lines;
using ParticleEngine2D;
using System.Xml;
using System.Xml.Linq;

namespace MoveIt
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Ball m_Ball;
        CInput m_Input;
        
        List<CAnimationObject> m_List;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = GlobalSetting.SCREEN_WIDTH;
            graphics.PreferredBackBufferHeight = GlobalSetting.SCREEN_HEIGHT;

            graphics.IsFullScreen = true;
            TargetElapsedTime = TimeSpan.FromTicks(166667);
            InactiveSleepTime = TimeSpan.FromSeconds(1);
            m_List = new List<CAnimationObject>();
        }
        void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
        {
            e.GraphicsDeviceInformation.PresentationParameters.PresentationInterval = PresentInterval.One;
        }
        protected override void Initialize()
        {
            
            CResourceManager.GetInstance().Init(Content);
            
            m_Ball = new Ball(new Vector2(120, 20));
            m_Input = new CInput();
            
            m_List.Add(new HardLine(new Vector2(100, 750), IDResource.BODY_LINE_LEFT));
            m_List.Add(new HardLine(new Vector2(120, 750), IDResource.BODY_LINE_LEFT));
            m_List.Add(new HardLine(new Vector2(140, 750), IDResource.BODY_LINE_LEFT));
            m_List.Add(new HardLine(new Vector2(160, 750), IDResource.BODY_LINE_LEFT));

            m_List.Add(new HardLine(new Vector2(160, 900), IDResource.BODY_LINE_LEFT));
            m_List.Add(new HardLine(new Vector2(180, 900), IDResource.BODY_LINE_LEFT));
            m_List.Add(new HardLine(new Vector2(200, 900), IDResource.BODY_LINE_LEFT));
            m_List.Add(new HardLine(new Vector2(220, 900), IDResource.BODY_LINE_LEFT));
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        protected override void UnloadContent()
        {
            
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            m_Ball.UpdateMovement(gameTime, m_Input);
            m_Ball.UpdateAnimation(gameTime, m_Input);
            
            for (int i = 0; i < m_List.Count; i++)
            {
                m_List[i].UpdateAnimation(gameTime, m_Input);
                m_List[i].UpdateMovement(gameTime, m_Input);
                m_Ball.UpdateCollision(m_List[i]);
            }
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            
            spriteBatch.Begin();
            m_Ball.Draw(spriteBatch);
            for (int i = 0; i < m_List.Count; i++)
            {
                m_List[i].Draw(spriteBatch);
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
